package si.unimb.src.wifisms;

import java.util.ArrayList;
import java.util.Arrays;

import si.unimb.src.wifisms.control.Database;
import si.unimb.src.wifisms.ui.MainActivityArrayAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Začetni razred.
 * @author perox
 */
public class mainActivity extends Activity implements OnItemClickListener {
	
	private ListView MenuList;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        MenuList = (ListView)findViewById(R.id.MenuList);
	    MenuList.setOnItemClickListener(this);
	    
    }

	@Override
	protected void onResume() {
		
		super.onResume();
		
		/* v element menija vstavimo število čakajočih sporočil */
		ArrayList<String> StartMenuElements = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.startMenuItems)));
        StartMenuElements.set(1, String.format(StartMenuElements.get(1), Database.getMessages(this).size()));
		
		MainActivityArrayAdapter adapter = new MainActivityArrayAdapter(this, R.layout.main_list_view_with_icons, StartMenuElements);
		MenuList.setAdapter(adapter);
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		
		Intent intent;
		
		AlertDialog.Builder adb = new AlertDialog.Builder(mainActivity.this);
		adb.setTitle("Selected Item");
		
		/* kateri element v meniju je bil pristisnjen */
		switch (position) {
		case 0:
			intent = new Intent(view.getContext(), SendSms.class);
	        startActivityForResult(intent, 0);
			break;
		case 1:
			intent = new Intent(view.getContext(), WaitingMessages.class);
			startActivityForResult(intent, 0);
			break;
		case 2:
			intent = new Intent(view.getContext(), Accounts.class);
			startActivityForResult(intent, 0);
			break;
		case 3:
			intent = new Intent(view.getContext(), Settings.class);
			startActivityForResult(intent, 0);
			break;
		case 4:
			intent = new Intent(view.getContext(), About.class);
			startActivityForResult(intent, 0);
			break;

		default:
			adb.setMessage("Selected Item is = "+MenuList.getItemAtPosition(position));
			adb.setPositiveButton("Ok", null);
			adb.show();
			break;
		}
	}
}